package com.bsoft.exchange.service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.bsoft.exchange.domain.CanceledWorksDomain;
import com.bsoft.exchange.pojo.CanceledWorksPojo;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bsoft.exchange.dao.his.CanceledWorksMapper;
import com.bsoft.exchange.domain.CanceledWorks;

@Service
public class CanceledWorksService {
	
	@Autowired
	CanceledWorksMapper canceledWorksmapper;
	
	
	/**
	 * 由运营商调用，中心号源系统返回已停诊的排班信息。建议每小时调用一次。
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<CanceledWorks> selectList(String startTime,String endTime) {
		
		SimpleDateFormat strat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date parse1 = null;
		Date parse2 = null;
		try {
			parse1 = strat.parse(startTime+" 00:00:00");
			parse2 = strat.parse(endTime+" 23:59:59");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		List<CanceledWorks> canceledWorks = canceledWorksmapper.selectList(parse1,parse2);
		return canceledWorks;
		
	}

/**
 * 获取停诊信息
 */
	public BaseResponse<List<CanceledWorksDomain>>getCanceledWorksInfo(CanceledWorksPojo canceledWorksPojo){
		BaseResponse<List<CanceledWorksDomain>> listBaseResponse=new BaseResponse<>();
		List<CanceledWorksDomain> dataList=canceledWorksmapper.selectCanceled(canceledWorksPojo.getOrganizationId(),canceledWorksPojo.getPeriodId(),
				canceledWorksPojo.getScheduleDate(),canceledWorksPojo.getDepartmentId(),canceledWorksPojo.getDoctorId());
		if (dataList==null){
			listBaseResponse.setMessage("查询数据库失败");
			listBaseResponse.setCode(Constant.errorTag);
		}else{
			for (CanceledWorksDomain data:dataList) {
				if (!TextUtils.isEmpty(data.getJZKH())){
					data.setPatientMedicalCardNumberType(2);
					data.setPatientMedicalCardNumber(data.getJZKH());
				}else if (!TextUtils.isEmpty(data.getYBKH())){
					data.setPatientMedicalCardNumberType(3);
					data.setPatientMedicalCardNumber(data.getYBKH());
				}else{
					data.setPatientMedicalCardNumberType(5);
					data.setPatientMedicalCardNumber(data.getMZHM());
				}
				data.setCardType("01");
				data.setCardNumber(data.getSFZH());
			}
			listBaseResponse.setMessage("请求成功");
			listBaseResponse.setCode(Constant.successResponse);
			listBaseResponse.setData(dataList);
		}
	return listBaseResponse;
	}
}
